<template>
  <div>
    <el-button plain type="primary" @click="dialogFormVisible = true"
      >新增放映厅</el-button
    >
    <el-divider content-position="left">放映厅列表</el-divider>

    <el-table :data="rooms">
      <el-table-column
        label="放映厅名称"
        align="center"
        prop="room_name"
      ></el-table-column>
      <el-table-column
        label="放映厅类型"
        align="center"
        prop="room_type"
      ></el-table-column>
      <el-table-column label="放映厅座位数" align="center">
        <template slot-scope="scope">
          <span v-if="scope.room_size == null">暂未配置坐位</span>
          <span else>{{ scope.room_size }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px" align="center">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="success"
            icon="el-icon-view"
            circle
            @click="$router.push('/home/showingon-plan-list/' + scope.row.id)"
          ></el-button>
          <el-button
            size="small"
            type="info"
            icon="el-icon-plus"
            circle
            @click="$router.push('/home/showingon-plan-add/' + scope.row.id)"
          ></el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            circle
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 放映厅弹框 -->
    <el-dialog title="新增放映厅" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="放映厅名称" label-width="130px" prop="room_name">
          <el-input v-model="form.room_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="放映厅类型" label-width="130px" prop="room_type">
          <el-select v-model="form.room_type" placeholder="请选择放映厅类型">
            <el-option
              v-for="item in types"
              :key="item.id"
              :label="item.type_name"
              :value="item.type_name"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import httpApi from "@/http";
export default {
  data() {
    return {
      dialogFormVisible: false, //控制弹框Dialog显示与隐藏
      form: {
        room_name: "",
        room_type: "",
        movie_cinema_id: this.$route.params.cinemaId,
      },
      types: [], //保存所有的影院类型
      rules: {
        room_name: [
          { required: true, message: "请输入放映厅名称", trigger: "blur" },
        ],
        room_type: [
          { required: true, message: "请选择放映厅类型", trigger: "blur" },
        ],
      },
      rooms: [
        {
          room_name: "",
          room_type: "",
          room_size: "",
        },
      ],
    };
  },
  methods: {
    // 删除放映厅
    del(cinemaRoomId) {
      console.log('aaa')
      this.$confirm("此操作将永久删除该放映厅, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let id = cinemaRoomId;
          httpApi.cinemaRoomApi.delete({id}).then((res) => {
            console.log('删除放映厅',res);
            this.list();
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 根据电影院ID查询该影院放映厅
    list() {
      httpApi.cinemaRoomApi
        .list({ cinema_id: this.$route.params.cinemaId })
        .then((res) => {
          console.log(res);
          this.rooms = res.data.data;
        });
    },
    // 提交新增放映厅
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          httpApi.cinemaRoomApi.add(this.form).then((res) => {
            if (res.data.code == 200) {
              this.$message.success("放映厅添加成功");
              this.dialogFormVisible = false;
              this.$refs["form"].resetFields();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      console.log(this.form);
      this.list();
    },
    // 初始化电影类型
    initRoomTypes() {
      httpApi.cinemaRoomApi.queryAllTypes().then((res) => {
        this.types = res.data.data;
      });
    },
  },
  mounted() {
    // 初始化放映厅类型列表
    this.initRoomTypes();
    this.list();
  },
};
</script>

<style lang="scss" scoped>
</style>